local Widget = require "widgets/widget"
local Text = require "widgets/text"
local Image = require "widgets/image"
local TextButton = require "widgets/textbutton"

local Skill_Menu =
	Class(
	Widget,
	function(self)
		Widget._ctor(self, "Skill_Menu")

		self.height = 800
		self.width = 500

		self.windowroot = self:AddChild(Widget("windowroot"))

		self.bg = self.windowroot:AddChild(Image("images/musha_panel.xml", "musha_panel.tex"))
		self.bg:SetSize(self.width, self.height)
		self.root = self.bg:AddChild(Widget("root"))

		--这里很玄学，如果最后一位不设置0，y会指向相同定义域下一个变量的内存地址
		local position_x = {0, 0, -100, -100, -100, -100, -100, -100, -100, 0, -100, -100, -100, 0}
		local position_y = {235, 200, 170, 140, 110, 80, 50, 20, -10, -45, -75, -105, -135, 0}
		local skill_title = {
			"SKILLSTITTLE",
			"ACTIVE",
			"SLEEP",
			"SNIFF",
			"MUSIC",
			"SHADOWHIDE",
			"ELECTRICATTACK",
			"ELECTRICSHIELD",
			"POWERATTACK",
			"PASSIVE",
			"Valkyr",
			"Berserk",
			"FIRESHIELD",
			""
		}
		--1睡觉，2嗅觉，3音乐，4隐身，5电击，6电盾，7重击，8女武神状态，9狂暴状态
		self.skill_point_max = {
			Sleep = 1,
			Sniff = 2,
			Music = 2,
			ShadowHide = 3,
			ElectricAttack = 3,
			ElectricShield = 5,
			PowerAttack = 5,
			Valkyr = 5,
			Berserk = 3
		}

		for k, v in pairs(skill_title) do
			self.root:AddChild(Text(BODYTEXTFONT, 30, STRINGS.MUSHA_SKILLS[string.upper(v)])):SetPosition(
				position_x[k],
				position_y[k],
				0
			)
		end

		self.value_button = {}
		local position_y2 = {
			Sleep = 170,
			Sniff = 140,
			Music = 110,
			ShadowHide = 80,
			ElectricAttack = 50,
			ElectricShield = 20,
			PowerAttack = -10,
			Valkyr = -75,
			Berserk = -105
		}
		local skill_name = deepcopy(skill_title)
		table.remove(skill_name, 1)
		table.remove(skill_name, 2)
		table.remove(skill_name, 10)
		for k, v in pairs(self.skill_point_max) do
			self.value_button[k] = self.root:AddChild(TextButton())
			self.value_button[k]:SetText(0)
			self.value_button[k]:SetPosition(100, position_y2[k], 0)
			self.value_button[k]:SetOnClick(
				function()
					local value_old = self.value_button[k].text:GetString()
					local skill_point_old = tonumber(string.sub(value_old, 2, string.find(value_old, "/") - 1))
					if self.skill_point_new["Unassigned"] > 0 then
						if skill_point_old < v then
							self.value_button[k]:SetText(string.format("[%d/%d]", skill_point_old + 1, v))
							self.skill_point_unassigned:SetString(
								STRINGS.MUSHA_SKILLS.UNASSIGNED .. "  " .. self.skill_point_new["Unassigned"] - 1
							)
							self.skill_point_new[k] = self.skill_point_new[k] + 1
							self.skill_point_new["Unassigned"] = self.skill_point_new["Unassigned"] - 1
						end
					else
						self.caller.components.talker:Say(STRINGS.MUSHA_SKILLS.MUSHA_TALK_NEED_LEVEL)
					end
				end
			)
		end

		self.skill_point_unassigned = self.root:AddChild(Text(BODYTEXTFONT, 30, "0"))
		self.skill_point_unassigned:SetPosition(-90, -185, 0)

		self.treasure_count = self.root:AddChild(Text(BODYTEXTFONT, 30, "0/0"))
		self.treasure_count:SetPosition(85, -185, 0)

		self.save_button = self.root:AddChild(TextButton())
		self.save_button:SetPosition(-100, -230, 0)
		self.save_button:SetTextSize(45)
		self.save_button:SetText(STRINGS.MUSHA_SKILLS.SAVE)
		self.save_button:SetOnClick(
			function()
				self:OnSave()
			end
		)

		self.cancel_button = self.root:AddChild(TextButton())
		self.cancel_button:SetPosition(100, -230, 0)
		self.cancel_button:SetTextSize(45)
		self.cancel_button:SetText(STRINGS.MUSHA_SKILLS.CANCEL)
		self.cancel_button:SetOnClick(
			function()
				self:Hide()
			end
		)

		self:Hide()
	end
)

function Skill_Menu:OnSave()
	for k, v in pairs(self.skill_point) do
		if self.skill_point[k] ~= self.skill_point_new[k] then
			self.caller.replica.musha_skills:SetData(k, self.skill_point_new[k])
		end
	end
	self:Hide()
end

function Skill_Menu:ReFlash(caller, data)
	self.caller = caller
	self.skill_point = {}

	for k, v in pairs(self.skill_point_max) do
		self.skill_point[k] = caller.replica.musha_skills.skill_point[k]:value()
		self.value_button[k]:SetText(string.format("[%d/%d]", self.skill_point[k], v))
	end
	self.skill_point["Unassigned"] = caller.replica.musha_skills.skill_point["Unassigned"]:value()
	self.skill_point_new = deepcopy(self.skill_point)

	self.skill_point_unassigned:SetString(STRINGS.MUSHA_SKILLS.UNASSIGNED .. "  " .. self.skill_point["Unassigned"])
	self.treasure_count:SetString(
		STRINGS.MUSHA_SKILLS.TREASURE .. data.found_treasure_count .. "/" .. data.all_treasure_count
	)
end

return Skill_Menu
